package org.micode.libs.webflux.helper;

import org.micode.common.domain.base.BaseUserCache;
import org.micode.common.domain.vo.ConvertFileVO;
import org.micode.common.domain.vo.FileVO;
import org.micode.common.file.FileResponseVO;
import org.micode.common.file.FileUploadVO;
import org.micode.common.file.TargetPathHelper;
import org.micode.common.file.UploadFileTypeEnum;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.util.function.Function;

/**
 * 文件下载上传等的处理
 */
public class WebFluxFileHelper {

    /**
     * 文件上传
     *
     * @param filePart
     * @param userType
     * @param fileType
     * @param bizType
     * @param langType
     * @param currentUser
     * @param targetPathHelper
     * @param doSaveFileInfo
     * @param doConvertFile
     * @return
     * @throws IOException
     */
    public static FileResponseVO uploadFile(FilePart filePart,
                                            Integer userType,
                                            UploadFileTypeEnum fileType,
                                            String bizType,
                                            String langType,
                                            BaseUserCache currentUser,
                                            TargetPathHelper targetPathHelper,
                                            Function<FileUploadVO, Integer> doSaveFileInfo,
                                            Function<ConvertFileVO, Integer> doConvertFile) throws IOException {
        return FileTransferHelper.uploadFile(filePart,
                userType,
                fileType,
                bizType,
                langType,
                currentUser,
                targetPathHelper,
                doSaveFileInfo,
                doConvertFile);
    }

    /**
     * 文件下载
     *
     * @param fileVO
     * @param response
     * @return
     */
    public static Mono<Void> downloadFile(FileVO fileVO,
                                          ServerHttpResponse response) {
        return FileTransferHelper.downloadFile(fileVO, response);
    }

}
